package mspbots.teams.client.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.minio.MinioClient;
import lombok.extern.log4j.Log4j2;
import mspbots.teams.client.core.client.TeamsClient;
import mspbots.teams.common.RabbitKeys;
import mspbots.teams.common.ResultMsg;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.web.reactive.function.client.WebClient;

/**
 * @author billb
 */
@Log4j2
public abstract class AbstractToolsUtil {

    protected ReactiveStringRedisTemplate stringRedisTemplate;
    protected ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;
    protected ObjectMapper objectMapper;

    protected TeamsClient teamsClient;
    protected MinioClient minioClient;
    protected WebClient dataClient;

    private RabbitMessagingTemplate rabbitMessagingTemplate;

    protected void sendEntity(String key, ResultMsg resultMsg) {
        this.rabbitMessagingTemplate.convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME, key, resultMsg);
    }

    @Autowired
    public void setDataClient(WebClient.Builder builder,
                              ReactorLoadBalancerExchangeFilterFunction filterFunction) {
        this.dataClient = builder.baseUrl("http://data-teams/")
                .filter(filterFunction)
                .build();
    }

    @Autowired
    public void setTeamsClient(TeamsClient teamsClient) {
        this.teamsClient = teamsClient;
    }

    @Autowired
    public void setMinioClient(MinioClient minioClient) {
        this.minioClient = minioClient;
    }

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setMessagingTemplate(RabbitMessagingTemplate messagingTemplate) {
        this.rabbitMessagingTemplate = messagingTemplate;
    }

    @Autowired
    public void setStringRedisTemplate(ReactiveStringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setReactiveRedisTemplate(ReactiveRedisTemplate<String, Object> redisTemplate) {
        this.reactiveRedisTemplate = redisTemplate;
    }


}
